
var Eui = {}

//导出bill的html形式
Eui.getBillHtml = function(divId,params){
	Eui.getHtmlPrint(divId,'bill',params)
}

//导出bill
Eui.exportBill = function(params){

	Eui.exportPrint('bill',params)
}


//导出crosstab
Eui.getCrosstabHtml = function(divId,params){
	Eui.getHtmlPrint(divId,'crosstab',params)
}

//导出crosstab
Eui.exportCrosstabReport = function(params){
	Eui.exportPrint('crosstab',params)
}

//导出crosstab
Eui.getHtmlPrint = function(divId,path,params){
	if (!params.queryName) alert('must support queryName')
	//var path = contextRoot + "d/"+path+"/"+ params.queryName +".html";
	//这里为何不一致？
	var path = "/d/"+path+"/"+ params.queryName +".html";
	var query = "";
	var prop
	for (prop in params) {
		if (typeof params[prop] != "function"&&params[prop]) {
			query += prop + "=" + encodeURIComponent(params[prop]) + "&";
		}
	}
	if (query!="")
		path += "?"+query
	Rpc.loadPage(divId,path,null,{blockScreen:true});
}

Eui.exportPrint = function(path,params){
	if (!params.queryName) alert('must support queryName')
	var type = params.exportType
	var path = contextRoot + 'd/'+path+'/'+ params.queryName +'.'+type
	var query = "";
	var prop
	for (prop in params) {
		if (typeof params[prop] != "function"&&params[prop]) {
			query += prop + "=" + encodeURIComponent(params[prop]) + "&";
		}
	}
	if (query!="")
		path += '?'+query
	if (type=='pdf'||type=='xls'||type=='rtf'){
		document.location.href=path
	}else if (type=='xml'||type=='html')
		window.open(path)
}

/* 显示导航数据 */
Eui.showNavigation = function(){
	var nav = $("nav");
	if (!nav) return;
	if (parent.pageType=='dialog'){
		nav.parentNode.style.display = "none";
		return;
	}
	var path = "";
	if(parent.$("frameset1")){
		path = parent.$("frameset1").longdesc;
	}
	else{
		return;
	}
	nav.innerHTML = "<span class=dot>&nbsp;&nbsp;&nbsp;&nbsp;</span>"+path;
}

/**
 * 缺省的onDelete方法
 */
Eui.onDel = function(service,items,entityName,desc,refFunc){	
	if (!items)
		showWarning("请选择需要删除的"+entityName+"。");
	else{
		if(showQuestion("是否删除这些"+entityName+(desc?desc:"？"))){
			DWR.call(service,items,function(){
				showInformation("删除成功！");
				if (refFunc){
					refFunc();//刷新页面
				}
			},{blockScreen:true});
		}
	}
}

/* 生成tabdiv
 * @param divId 生成tab的div id
 * @param tabs tab数组或者map 每个tab数据格式为
 *        {name:"tab_xxx",text:"示例tab",tabFunc:function(){alert("点击tab触发的方法")}}
 */
Eui.buildTabs = function(divId,tabs){
	var div = $(divId);
	if (!(tabs instanceof Array))
	{
		var newArray = []
		for (prop in tabs){
			if (prop == "toJSONString") continue;	//DWR.js中为Object增加的属性
			newArray.push(tabs[prop]);
		}
		tabs = newArray;
	}
	for (var i = 0;i<tabs.length;i++){
		var tab = tabs[i];
		var a = document.createElement("A");
		a.id = tab.name;
		a.tabFunc = typeof tab.tabFunc == "function"?tab.tabFunc:function(){};
		a.onclick = function(){
			try{
				this.tabFunc();
			}catch(e){showError(e.description);return false;}
			Eui.switchTab(this);
			return false;
		}
		a.href = "#";
		if (i == 0)
			a.className = "treeTabc";
		else
			a.className = "treeTab";
		a.innerHTML = tab.text;
		div.appendChild(a);
	}
}

//被选中的tab样式变化方法
Eui.switchTab = function(tab){
	if (!tab) return;
	var tabs = tab.parentNode.childNodes;
	for (var i = 0;i<tabs.length;i++){
		var t = tabs[i];
		if (t.id == tab.id){
			t.className = "treeTabc"
		}else{
			t.className = "treeTab"
		}
	}
}
/** 统计报表 查询 条件 */
Eui.showStatBtn = function(divId,headId,options){
	if (!options) options = {}
	if (!options.form) return;
	var form = options.form;
	var showHead = function(){
		var statType = Common.getRadioValue("statType");
		var statClass = Common.getRadioValue("statClass");
		if (!headId) return;
		$("statTime").innerHTML = Format.today(true);
		$("statoper").innerHTML = Common.cOperator().name;
		$("statorg").innerHTML = Common.cOrg().name;
		$("statClassSpan").innerHTML = statClass?Common.getText('stat_class',statClass):"无";
		//$("statTimeType").innerHTML = "业务时间";
		$("beginTime").innerHTML = "";
		$("endTime").innerHTML = "";
		if(statType == Constants.CLEAR_STAT_DYNA){
			var sDate = form.beginDate.value;
			var eDate = form.endDate.value;
			$("beginTime").innerHTML = Format.getBeginTimeOfDay(sDate);
			$("endTime").innerHTML = Format.getEndTimeOfDay(eDate);
			if(!checkTimeOrder(form.beginDate,form.endDate))
			return false;
		}else if(statType == Constants.CLEAR_STAT_DAY){
			var sDate = form.statDate.value;
			$("beginTime").innerHTML = Format.getBeginTimeOfDay(sDate);
			$("endTime").innerHTML = Format.getEndTimeOfDay(sDate);
		}else if(statType == Constants.CLEAR_STAT_MONTH){
			var sDate = form.statDate.value;
			$("beginTime").innerHTML = Format.getBeginTimeOfMonth(sDate);
			$("endTime").innerHTML = Format.getEndTimeOfMonth(sDate);
		}else if(statType == Constants.CLEAR_STAT_YEAR){
			var sDate = form.statDate.value;
			$("beginTime").innerHTML = Format.getBeginTimeOfYear(sDate);
			$("endTime").innerHTML = Format.getEndTimeOfYear(sDate);
		}
		return true;
	}
	Rpc.loadPage(divId,"/common/report/stat_btnview_div.html",function(){
		Eui.setStatPeriod();
		if (options.statFunc && typeof options.statFunc == "function")
			form.statBtn.onclick = function(){showHead();options.statFunc()};
		if (!headId) 
			return;
		Rpc.loadPage(headId,"/common/report/pivotable_head_div.html",function(){
			$("pivotable_head_title").innerHTML = document.title;
		});
	});
}

/**
 * 生成起止日期
 * @param divId 显示模板的区域id
 * @param options
 */
Eui.getDateInput = function(label,formName,inputName,checkType,cannotEmpty,withTime){
	var idStr = "";
	if(withTime)idStr += " id='"+inputName+"AndTime'";
	var d = "<label>"+label+"</label> <input type=\"text\" name=\""+inputName+"\" "+idStr+" onblur=\"checkV(this,'"+checkType+"',"+cannotEmpty+")\">";
		d += "<a href=\"javascript:void(0)\" title=\"选择"+label+"\" onclick=\"showDatePicker($('"+formName+"')['"+inputName+"']);\" class=\"date\">&nbsp;&nbsp;</a></span>";
	if(cannotEmpty){
		d += "<span class=des>*</span>";
	}
	return d;
}
Eui.getBeginDateInput = function(label,formName,beginName,endName,checkType,cannotEmpty){
	var d = "<label>"+label+"</label> <input type=\"text\" name=\""+beginName+"\" onblur=\"if(checkV(this,'"+checkType+"',"+cannotEmpty+"))checkTimeOrder($('"+formName+"')['"+beginName+"'],$('"+formName+"')['"+endName+"'])\">";
		d += "<a href=\"javascript:void(0)\" title=\"选择"+label+"\" onclick=\"showDatePicker($('"+formName+"')['"+beginName+"']);\" class=\"date\">&nbsp;&nbsp;</a>";
	if(cannotEmpty){
		d += "<span class=des>*</span>";
	}
	return d;
}
Eui.getEndDateInput = function(label,formName,beginName,endName,checkType,cannotEmpty){
	var d = "<label>"+label+"</label> <input type=\"text\" name=\""+endName+"\" onblur=\"if(checkV(this,'"+checkType+"',"+cannotEmpty+"))checkTimeOrder($('"+formName+"')['"+beginName+"'],$('"+formName+"')['"+endName+"'])\">";
		d += "<a href=\"javascript:void(0)\" title=\"选择"+label+"\" onclick=\"showDatePicker($('"+formName+"')['"+endName+"']);\" class=\"date\">&nbsp;&nbsp;</a>";
	if(cannotEmpty){
		d += "<span class=des>*</span>";
	}
	return d;
}
/**
 * 初始化单个日期输入框
 * @param targetId	 显示日期框的区域的id，如td的id
 * @param label		日期的label，如"完成日期"
 * @param formName	 表单的id
 * @param inputName	生成日期输入框的name
 * @param checkType	校验类型			   
 * @param cannotEmpty  是否可以为空  true|false
*/
Eui.initDateInput = function(targetId,label,formName,inputName,checkType,cannotEmpty){
	$(targetId).innerHTML = Eui.getDateInput(label,formName,inputName,checkType,cannotEmpty);
}
/**
 * 初始化开始日期输入框
 * @param targetId	 显示日期框的区域的id，如td的id
 * @param label		日期的label，如"完成日期"
 * @param formName	 表单的id
 * @param beginName	生成开始日期输入框的name
 * @param endName	  生成结束日期输入框的name
 * @param checkType	校验类型			   
 * @param cannotEmpty  是否可以为空  true|false
*/
Eui.initBeginDateInput = function(targetId,label,formName,beginName,endName,checkType,cannotEmpty){
	$(targetId).innerHTML = Eui.getBeginDateInput(label,formName,beginName,endName,checkType,cannotEmpty);
}
/**
 * 初始化结束日期输入框（参数同上）
*/
Eui.initEndDateInput = function(targetId,label,formName,beginName,endName,checkType,cannotEmpty){
	$(targetId).innerHTML = Eui.getEndDateInput(label,formName,beginName,endName,checkType,cannotEmpty);
}

/**
 * 设置统计周期
 */
Eui.setStatPeriod = function(){
	var form = Page.form;
	var formName = form.id;//"statForm";
	var st = Common.getRadioValue("statType");
	if(st == Constants.CLEAR_STAT_DYNA){
		var beStr = "<table border=0 cellspacing=0 cellpadding=0 width=100% class=col2><tr><td>";
			beStr += Eui.getBeginDateInput("起始日期",formName,"beginDate","endDate","_pastDate",true);
			beStr += "</td><td>";
			beStr += Eui.getEndDateInput("结束日期",formName,"beginDate","endDate","_pastDate",true);
			beStr += "</td></tr></table>";
		$("statDateTd").innerHTML = beStr;
		form.beginDate.value = Format.today();
		form.endDate.value = Format.today();
	}
	else{
		$("statDateTd").innerHTML = Eui.getDateInput("统计日期",formName,"statDate","_pastDate",true);
		form.statDate.value = Format.today();
	}
}
/**
 * 生成大小关联的输入框
*/
Eui.getCompareInputA = function(formName,labelA,labelB,inputNameA,inputNameB,checkType,compareType,cannotEmpty){
	var str = "<label>"+labelA+"</label> <input type=\"text\" name=\""+inputNameA+"\" onblur=\"if(checkDubleObjs($('"+formName+"')['"+inputNameA+"'],$('"+formName+"')['"+inputNameB+"'],'"+checkType+"','"+checkType+"',"+cannotEmpty+","+cannotEmpty+"))compareValue($('"+formName+"')['"+inputNameA+"'],$('"+formName+"')['"+inputNameB+"'],'"+compareType+"','"+labelA+"','"+labelB+"')\">";
	if(cannotEmpty){
		str += "<span class=des>*</span>";
	}
	str += " ";
	return str;
}
Eui.getCompareInputB = function(formName,labelA,labelB,inputNameA,inputNameB,checkType,compareType,cannotEmpty){
	var str = "<label>"+labelB+"</label> <input type=\"text\" name=\""+inputNameB+"\" onblur=\"if(checkDubleObjs($('"+formName+"')['"+inputNameA+"'],$('"+formName+"')['"+inputNameB+"'],'"+checkType+"','"+checkType+"',"+cannotEmpty+","+cannotEmpty+"))compareValue($('"+formName+"')['"+inputNameA+"'],$('"+formName+"')['"+inputNameB+"'],'"+compareType+"','"+labelA+"','"+labelB+"')\">";
	if(cannotEmpty){
		str += "<span class=des>*</span>";
	}
	str += " ";
	return str;
}
Eui.initCompareInput = function(formName,targetIdA,targetIdB,labelA,labelB,inputNameA,inputNameB,checkType,compareType,cannotEmpty){
	$(targetIdA).innerHTML = Eui.getCompareInputA(formName,labelA,labelB,inputNameA,inputNameB,checkType,compareType,cannotEmpty);
	$(targetIdB).innerHTML = Eui.getCompareInputB(formName,labelA,labelB,inputNameA,inputNameB,checkType,compareType,cannotEmpty);
}